<?php
namespace app\admin\controller;

use app\common\controller\AdminBaseController;
use think\Db;

/**
 * 后台首页
 * @author Kaisa
 * @version 2017-02-09 17:00:00
 */
class IndexController extends AdminBaseController {
	
	/**
	 * 首页
	 */
	public function index(){
		$version = Db::query('SELECT VERSION() AS ver');
		$info  = [
            'url'             => $_SERVER['HTTP_HOST'],
            'document_root'   => $_SERVER['DOCUMENT_ROOT'],
            'server_os'       => PHP_OS,
            'server_port'     => $_SERVER['SERVER_PORT'],
            'server_ip'       => $_SERVER['SERVER_ADDR'],
            'server_soft'     => $_SERVER['SERVER_SOFTWARE'],
            'php_version'     => PHP_VERSION,
            'mysql_version'   => $version[0]['ver'],
            'max_upload_size' => ini_get('upload_max_filesize')
        ];
        $info=implode(',', $info); 
        return $this->fetch('index', ['info' => $info]);
	}
	
	/**
     * 更新
     */
    public function update() {
        if (request()->isPost()) {
        	
        }else{
        	$user = db('user')->find(session('admin_id'));
        	return $this->fetch('update', ['user' => $user]);
        }
    }
	
	/**
     * 更新密码
     */
    public function password() {
        if (request()->isPost()) {
	      	$data = request()->post();
			if(empty($data['old_pwd'])){
				$this->error('请输入旧密码');
			}
			if(empty($data['login_pwd']) || empty($data['login_pwd2'])){
				$this->error('请输入新密码');
			}
			if($data['login_pwd'] != $data['login_pwd2']){
				$this->error('二次密码输入不一致');
			}
            $user = db('user')->find(session('admin_id'));
			if(md5_salt($data['old_pwd'])!=$user['login_pwd']){
				$this->error('旧密码错误');
			}
            $update_data['login_pwd'] = md5_salt($data['login_pwd']);
            if (db('user')->where('id',session('admin_id'))->update($update_data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }else{
        	$user = db('user')->find(session('admin_id'));
        	return $this->fetch('password', ['user' => $user]);
        }
    }
	
	/**
     * 清除缓存
     */
    public function clear() {
        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }
	
}
